boolean firstPress = false;
int x0 ;
int y0 ;
int x1 ;
int y1 ;

void setup() {
  size(640, 360);
  background(255);
  noFill();
}

void mousePressed() {
  if (firstPress) {
    x1 = mouseX;
    y1 = mouseY;
    firstPress = false;
    stroke(random(1,250),random(1,250),random(1,250));
  }
  else {
    x0 = mouseX;
    y0 = mouseY;
    firstPress = true;
  }
}

void draw() {
  if (firstPress) {
    background(255);
    x1 = mouseX;
    y1 = mouseY;

    if(x0 == x1){
      float y = min(y0,y1);
      float x = x0;
      while (y<max(y0,y1)) {
         point(round(x), round(y));
         y += 1;
       }
    }else{
      float x;
      float endX;
      float y;
      float m;
      boolean up;
       
       
      if(x0<x1){
         x = x0;
         endX = x1;
         y = y0;
         m = float(y1-y0)/float(x1-x0);
         up = y1>y0;  
       }else{
         x = x1;
         endX = x0;
         y = y1;
         m = float(y0-y1)/float(x0-x1);
         up = y0>y1;  
       }
       
       while (x<endX) {
         point(round(x), round(y));
         y+=(abs(m)<=1)?m:(up)?1:-1;
         x+=(abs(m)<=1)?1:((up)?1.0:-1.0)/m;
       }
    }

   
  }
}


